/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.format;

import info.xmlprint.core.format.Format;
import info.xmlprint.ui.message.GuiMessages;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Spinner;

public class LanguageComposite extends GuiCommon implements IFormatSetup {
	private Composite comp;
	private Group groupLanguageSelection;

	private String[] itemListHyphLanguage_local;
	private Combo comboHyphenationLanguage;
	private Spinner hyphLeftMin, hyphRightMin;
	private Button buttonHyphenation;
	
	private Format format, formatForSetup;
	private String standardReference;
	private boolean isFormatStandard;

	public LanguageComposite(org.eclipse.swt.widgets.Composite parent,
			int style, Format f) {
		format = f;
		formatForSetup = f.objClone();

		comp = new Composite(parent, style);

		languageInit();

		initGUI();
	}

	private void languageInit() {
		{
			String[] original = new String[] { "de:DR", "de:1901", "en:GB", "en:US", "fr:fr", "grc:grc", "el:el", "el:Polyton", "it:it", "la:la", "es:es" };

			itemListHyphLanguage_local = new String[] {
					GuiMessages.Stileditor_Hyphenation_Language_German_New,
					GuiMessages.Stileditor_Hyphenation_Language_German_Old,
					GuiMessages.Stileditor_Hyphenation_Language_English_British,
					GuiMessages.Stileditor_Hyphenation_Language_English_American,
					GuiMessages.Stileditor_Hyphenation_Language_French,
					GuiMessages.Stileditor_Hyphenation_Language_Greek_Ancient,
					GuiMessages.Stileditor_Hyphenation_Language_Greek_Modern_Mono,
					GuiMessages.Stileditor_Hyphenation_Language_Greek_Modern_Poly,
					GuiMessages.Stileditor_Hyphenation_Language_Italian,
					GuiMessages.Stileditor_Hyphenation_Language_Latin,
					GuiMessages.Stileditor_Hyphenation_Language_Spanish	};

			addInternationalMap("language", original,
					itemListHyphLanguage_local);
		}
	}

	/*
	 * Einstellungswerte lesen und Format aktualisieren
	 */
	@Override
	public Format getFormat() {
		String s;

		// wenn Format standardwerte übernehmen soll, werde mit
		// standardReference
		// gespeichert.
		standardReference = "%@%";
		isFormatStandard = false;
		if (this.format.getType().equals("standard")) {
			standardReference = "";
			isFormatStandard = true;
		}

		String attrName = null;

		{
			attrName = "hyphenate";
			setFormatNewValue(attrName, buttonHyphenation, new Boolean(buttonHyphenation.getSelection()).toString());
			/*if (buttonHyphenation.getSelection()) {
				this.format.setAttribute(attrName, "true");
			} else {
				this.format.setAttribute(attrName, "false");
				
			}*/
			// As there is no "standard" box yet, we will always export this value
			// this.format.setInherit(attrName, false);
		}
		
		{
			attrName = "language";
			s = readCombo(comboHyphenationLanguage);
			s = getOriginalValue(attrName, s);
			String[] s1 = s.split(":");
			if (s1[0].equals(s1[1])) //no country code given
				s1[1] = "";
			setFormatNewValue(attrName, comboHyphenationLanguage, s1[0]);
			setFormatNewValue("country", comboHyphenationLanguage, s1[1]);
		}
		
		{
			attrName = "hyphenation-remain-character-count";
			s = hyphLeftMin.getText();
			setFormatNewValue(attrName, hyphLeftMin, s);
		}
		
		{
			attrName = "hyphenation-push-character-count";
			s = hyphRightMin.getText();
			setFormatNewValue(attrName, hyphRightMin, s);
		}
		
		return this.format;
	}
	
	private void setFormatNewValue(String attrName, Control control,
			String controlValue) {
		if (!isFormatStandard
				&& controlValue.equals(this.format.getFormatGroup()
						.getStandardFormat().getAttribute(attrName))) {
			controlValue = standardReference;
		}

		if (!control.isEnabled()) {
			this.format.setInherit(attrName, true);
		} else {
			this.format.setInherit(attrName, false);
		}
		this.format.setAttribute(attrName, controlValue);
	}

	@Override
	public Composite getComp() {
		return comp;
	}
	
	private void initGUI() {
		comp.setLayout(new GridLayout(4, false));

		groupLanguageSelection = new Group(comp, SWT.NONE);

		groupLanguageSelection.setLayout(new GridLayout(4, false));
		groupLanguageSelection.setText(GuiMessages.Stileditor_Gui_Hyphenation);
		groupLanguageSelection.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 4,
				0, 0));
		
		
		{ //hyphenate
			buttonHyphenation = new Button(groupLanguageSelection, SWT.CHECK
				| SWT.LEFT);
			buttonHyphenation.setLayoutData(getGD(0, 2, 150, 0));
			buttonHyphenation
				.setText(GuiMessages.Stileditor_Hyphenation_Enable);
			
			buttonHyphenation.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					comboHyphenationLanguage.setEnabled(buttonHyphenation.getSelection());
					hyphLeftMin.setEnabled(buttonHyphenation.getSelection());
					hyphRightMin.setEnabled(buttonHyphenation.getSelection());
				}
			});
			
			buttonHyphenation.setSelection(format.getAttribute("hyphenate").equalsIgnoreCase("true"));
			
			// add inherit Checkbox
			Button inheritButton = new Button(groupLanguageSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupLanguageSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			HashMap<String, Control> buttonMap = new HashMap<String, Control>();
			buttonMap.put("hyphenate", buttonHyphenation);
								
			// Language and Country
			addLabel(groupLanguageSelection,
					GuiMessages.Stileditor_Hyphenation_Language + ":");

			comboHyphenationLanguage = new Combo(groupLanguageSelection, SWT.READ_ONLY);
			comboHyphenationLanguage.setLayoutData(getGD(0, 3, 250, 0)); // TODO: Check, whether narrow box under Linux

			comboHyphenationLanguage.setItems(itemListHyphLanguage_local);
			
			comboHyphenationLanguage.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent e) {
			        switch (comboHyphenationLanguage.getSelectionIndex()) {
			        	case 2: //English (US)
			        	case 3: //English (UK)
			        	case 4: //French
			        		hyphLeftMin.setSelection(2);
			        		hyphRightMin.setSelection(3);
			        		break;
			        	case 5: //Ancient Greek
			        	case 6: //Modern Greek (monotonic)
			        	case 7: //Modern Greek (Polytonic)
			        		hyphLeftMin.setSelection(1);
			        		hyphRightMin.setSelection(1);
			        		break;
			        	default:
			        		hyphLeftMin.setSelection(2);
			        		hyphRightMin.setSelection(2);
			        		break;
			        }
			      }
			});
			
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("language", new Control[] { comboHyphenationLanguage });
		
			controlsInit(standardButton, inheritButton, hm, buttonMap,
				formatForSetup);
				
			//Left min
			addLabel(groupLanguageSelection,
					GuiMessages.Stileditor_Hyphenation_Left_Min + ":");
			hyphLeftMin = new Spinner(groupLanguageSelection, SWT.NONE);
			hyphLeftMin.setLayoutData(getGD(0, 3, 50, 0));
			hyphLeftMin.setDigits(0);
			hyphLeftMin.setIncrement(1);
			hyphLeftMin.setMaximum(10);
			hyphLeftMin.setMinimum(1);
			
			hm = new HashMap<String, Control[]>();
			hm.put("hyphenation-remain-character-count", new Control[] { hyphLeftMin });
			controlsInit(standardButton, inheritButton, hm, buttonMap,
					formatForSetup);
				
			//Right min
			addLabel(groupLanguageSelection,
					GuiMessages.Stileditor_Hyphenation_Right_Min + ":");
			hyphRightMin = new Spinner(groupLanguageSelection, SWT.NONE);
			hyphRightMin.setLayoutData(getGD(0, 3, 50, 0));
			hyphRightMin.setDigits(0);
			hyphRightMin.setIncrement(1);
			hyphRightMin.setMaximum(10);
			hyphRightMin.setMinimum(1);

			hm = new HashMap<String, Control[]>();
			hm.put("hyphenation-push-character-count", new Control[] { hyphRightMin });
			controlsInit(standardButton, inheritButton, hm, buttonMap,
					formatForSetup);
		
			/*if (format.getAttribute("hyphenate").equalsIgnoreCase("true"))
				buttonHyphenation.setSelection(true);
			else 
				buttonHyphenation.setSelection(false);
			comboHyphenationLanguage.setEnabled(buttonHyphenation.getSelection());
			hyphLeftMin.setEnabled(buttonHyphenation.getSelection());
			hyphRightMin.setEnabled(buttonHyphenation.getSelection());*/
		}	
	}

	@Override
	public boolean controlValueVertify() {
		// TODO Auto-generated method stub
		return true;
	}

}
